<?

/*	GroupBasic OBJECT
*	Comment here
*	
*					*/


class GroupBasic extends MOD {

#	function ajax()		-> from MOD class

#	Construct creates object before html cycle. 
	public function construct() {

	}

#	HTML loop module request	
	public function output() {
		global $DB;
		
		$this->group = ( $this->vars->type )?
			$DB->reference( $this->vars->type, $this->vars->id, $this->vars->objtype ) : $DB->filter_direct( $this->vars->objtype );
	
		if( $this->action )
			$nodes = $this->{ "filter_" . $this->action }();
			
		if( $nodes ) {
			$this->addclass( $this->action );
			$result = implode( "\n", $nodes );
		}
	
		return $this->parse( $result ) . $this->render_js();
	}

#	Display group children
	private function filter_visual() {
		global $DB;
		
		$nodes = array("foo","bar");
		
		return $nodes;
	}
	
#	Show thumbnail presentation of group children
	private function filter_thumbs() {
		global $DB;
		$nodes = array();
		
		if( $this->group && $group = $DB->filter_direct( $this->vars->objtype, "ID in( ". implode( ",", $this->group ) ." ) && active order by rank desc, rand()", "ID" ))
			foreach( $group as $id ) {
				$object	= new OBJECT( $this->vars->objtype, $id );
				$img	= $object->media->get( "media.img" );
				$rank	= (int) $object->get( "rank" );
				$size	= ( $this->vars->sizetype == "dynamic" )? ceil( $this->vars->size*( 0.92 + $rank*0.08 )) . ".0" : $this->vars->size;

				if( $img->ID ) $nodes[] = "<img src='". engine_get( "url" ) ."/public.img?id={$img->ID}&sze=$size' alt=\"". $object->get( "name" ) ."\" />";
			}

		return $nodes;
	}
	
#	Show ranked visuals of group children
	private function filter_ranked() {
		global $DB;
		$ranks = array();
		
		if( $this->group && $group = $DB->filter_direct( $this->vars->objtype, "ID in( ". implode( ",", $this->group ) ." ) && active order by rank desc, rand()", "ID" ))
			foreach( $group as $id ) {
				$object	= new OBJECT( $this->vars->objtype, $id );
				$img	= $object->media->get( "media.img" );
				$rank	= (int) $object->get( "rank" );
				$prop	= ceil( $this->vars->size*( 0.9 + $rank*0.1 ));
				$size	= ( $this->vars->sizetype == "dynamic" )? "$prop.0" : $this->vars->size;
				$link	= ( $link = $object->values[ 'meta' ]->link )? (( strpos($link, "http" ) === 0)? $link : "http://$link" ): "";
				
				if( $img->ID ) $nodes[ $rank ][] = "<a href='$link' target='_blank'><div style='background-image:url(/public.img?id={$img->ID}&sze=$size); width:{$prop}px; padding-top:{$prop}px;'></div></a>";
			}
			
		if( count( $nodes ))
			foreach( $nodes as $rank ) $ranks[] = "<span class='rank'><center>". implode( "\n", $rank ) ."</center></span>";
		
		$ranks[] = "<div class='stable'></div>";
		
		return $ranks;
	}
	
	
#	group.stream output function
#	Deprecated. Use media stream -> group instead.
	
	// list of movies
	private function filter_stream() {
		global $DB;
	
		//return 'foo';
		$nodes 	= $DB->reference( 'group.stream', (int) $this->vars->id, 'media.stream' );
		$nodes	= $DB->filter_objects( 'media.stream', 'ID in ( ' . implode(',', $nodes ) . ') AND active order by rank desc');
				
		// make list
		$return[]	= '<ul id="movies" class="movies">';			
		foreach ( $nodes as $key => $movie ) {
		
			$return[] = '<li class="movie">';
			$return[] = '<p class="streamTitle">' . $movie->get('name') . '</p>';
			$return[] = '<div style="position: relative; background: url(/public.img?id=' .$movie->get('ID') . '&sze=' . $this->vars->width . '.' . $this->vars->height . ') no-repeat top center; 
				width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" class="thumb">';
			$return[] = '<div class="playIt" style="width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" id="' . $movie->get('ID') . '"></div>';
			$return[] = '</div>';
			$return[] = '<div style="position: relative; width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" class="stream hide">';
			$return[] = $this->render_movie( $movie->get('id'), $movie->get('source'), $this->vars->width, $this->vars->height);
			$return[] = '</div>';
			$return[] = '</li>';
		
		}
		$return[] = '</ul>';
		
		$this->js = "
			mod = new GroupBasic();
			mod.prepstream();
		";
		
		return $return;
	}	
	
	
#	filter social streams (think kriski)	
	private function filter_social_stream() {
		global $DB;
		
		$nodes = $DB->filter_objects( 'social.stream', 'active' . (( $this->vars->personal && $_SESSION['uid'] ) ? ' AND creator = ' . $_SESSION['uid'] : '') . ' ORDER BY ID desc' . (( $lim = $this->vars->limit ) ? ' LIMIT ' . $lim : '' ));
		
		$return[]	= '<ul id="movies" class="movies">';			
		foreach ( $nodes as $key => $movie ) {
		
			$return[] = '<li class="movie">';
			$return[] = '<div style="position: relative; width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" class="stream">';
			$return[] = $this->render_movie( $movie->get('id'), $movie->get('source'), $this->vars->width, $this->vars->height);
			$return[] = '</div>';
			$return[] = '</li>';
		
		}
		$return[] = '</ul>';
		
		return $return;
	}
	
	
	

#	extra methods for media.stream output
	
	// get link of movie
	function render_link( $id, $source ) {
	
		if ($source == 'youtube') {
			$url	= 'http://www.youtube.com/v/' . $id . '&hl=en_US&fs=1&';
		} else if ($source == 'vimeo') {
			$url	= 'http://vimeo.com/moogaloop.swf?clip_id=' . $id . '&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1"';
		}
		
		return $url;
	}
	
	
	// render movie
	function render_movie( $id, $source, $width = 480, $height = 385 ) {
	
		$url	= $this->render_link( $id, $source );
		
		$movie	= '<object width="' . $width . '" height="' . $height . '"><param name="movie" value="' . $url . '"></param>';
		$movie	.= '<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>';
		$movie	.= '<embed src="' . $url . '" type="application/x-shockwave-flash" 
			allowscriptaccess="always" allowfullscreen="true" width="' . $width . '" height="' . $height . '"></embed></object>';
			
		return $movie;
	}


	/////////////////////////////////////
	/////// MODULE AJAX CALLS (js output)
	
	function ajax_visual() {
		// THIS ONE GETS PARSED, SEND JSON STRINGS ONLY. 
		global $DB;
		
		
		return json_encode( $json );
	}

} 
?>